#include "namespace.h"
#include "htmlif.h"
#include "htmlforeach.h"
#include "componentcall.h"

Namespace::Namespace(const YYLTYPE& loc)
        : Node( loc )
{
}

Node* Namespace::resolve( const QString& name )
{
    Node* n = this;
    while( n )
    {
        Namespace* nspace = dynamic_cast<Namespace*>(n);
        if ( nspace )
        {
            Node* result = nspace->getEntry(name);
            if ( result )
                return result;
        }
        n = n->parentNode();
    }
    return 0;
}

void Namespace::liftNames()
{
    m_liftedNames.clear();
    liftNames(this);
}

void Namespace::liftNames(Node* p)
{
    if ( dynamic_cast<ComponentCall*>( p ) )
    {
        ComponentCall* c = dynamic_cast<ComponentCall*>( p );
        if ( !c->varName().isEmpty() )
        {
            m_liftedNames[c->varName()] = c;
        }
        return;
    }

    Node* n = p->firstChild();
    while( n )
    {
        if ( !dynamic_cast<HtmlIf*>(n) && !dynamic_cast<HtmlForeach*>(n) )
           liftNames(n);
        n = n->nextSibling();
    }
}

Node* Namespace::getLiftedEntry( const QString& name ) const
{
    return m_liftedNames[name];
}
